﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Common;
using System.ComponentModel.DataAnnotations;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Entity;

namespace TaoPortal.Areas.Admin.Models
{

    public class RoleModel : BaseModel
    {
        public AddRole Entity { get; set; }
        public string RoleTypeList { get; set; }

        public RoleModel()
        {

        }

        public void InitEntity(string id)
        {
            var rp = new UserRepository();
            var data = rp.GetRoleByID(id);
            Entity = new AddRole
            {
                ID = data.ID,
                Integral = data.Integral,
                RoleDesc = data.RoleDesc,
                RoleName = data.RoleName,
                RoleType = data.RoleType
            };
        }

        public void Init()
        {
            var data = SysEnum.ToList<SysEnum.RoleType>();
            RoleTypeList = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(data);
        }

        public List<TP_Role> GetRoleList()
        {
            var rp = new UserRepository();
            var data = rp.GetAllRole();
            foreach (var item in data)
            {
                item.RoleType = SysEnum.ConvertValue<SysEnum.RoleType>(item.RoleType);
            }
            return data;
        }

        public void DoAdd()
        {
            var rp = new UserRepository();
            Entity.ID = Guid.NewGuid().ToString();
            rp.AddRole(new TP_Role
            {
                ID = Guid.NewGuid().ToString(),
                Integral = Entity.Integral,
                RoleDesc = Entity.RoleDesc,
                RoleName = Entity.RoleName,
                RoleType = Entity.RoleType
            });
        }

        public void DoEdit()
        {
            var rp = new UserRepository();
            rp.EditRole(new TP_Role
            {
                ID = Entity.ID,
                Integral = Entity.Integral,
                RoleDesc = Entity.RoleDesc,
                RoleName = Entity.RoleName,
                RoleType = Entity.RoleType
            });
        }

        public int Delete(string id)
        {
            var rp = new UserRepository();
            return rp.IfDeleteRole(id);
        }

    }

    public class AddRole
    {
        public string ID { get; set; }
        [Required]
        [Display(Name = "角色名称")]
        public string RoleName { get; set; }
        [Display(Name = "积分")]
        public Nullable<int> Integral { get; set; }
        [Required]
        [Display(Name = "角色类型")]
        public string RoleType { get; set; }
        [Display(Name = "描述")]
        public string RoleDesc { get; set; }
    }
}